{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正则化技术，岭回归（L2正则化）\n",
    "import numpy as np\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0.02584225, 0.78498745, 0.5386813 , 1.19598919, 0.52132333,\n",
       "        1.0337085 , 0.60562432, 0.79415316, 0.28669322, 0.03158494,\n",
       "        0.68382933, 0.26533867, 0.00545791, 0.16905569, 0.23225162,\n",
       "        0.64996   , 0.85104691, 0.24372013, 0.68609212, 0.87913029]),\n",
       " array([[0.02584225],\n",
       "        [0.78498745],\n",
       "        [0.5386813 ],\n",
       "        [1.19598919],\n",
       "        [0.52132333],\n",
       "        [1.0337085 ],\n",
       "        [0.60562432],\n",
       "        [0.79415316],\n",
       "        [0.28669322],\n",
       "        [0.03158494],\n",
       "        [0.68382933],\n",
       "        [0.26533867],\n",
       "        [0.00545791],\n",
       "        [0.16905569],\n",
       "        [0.23225162],\n",
       "        [0.64996   ],\n",
       "        [0.85104691],\n",
       "        [0.24372013],\n",
       "        [0.68609212],\n",
       "        [0.87913029]]),\n",
       " array([[1.00000000e+00, 2.58422472e-02, 6.67821741e-04, 1.72580145e-05,\n",
       "         4.45985878e-07, 1.15252773e-08, 2.97839066e-10],\n",
       "        [1.00000000e+00, 7.84987450e-01, 6.16205297e-01, 4.83713425e-01,\n",
       "         3.79708968e-01, 2.98066774e-01, 2.33978677e-01],\n",
       "        [1.00000000e+00, 5.38681304e-01, 2.90177547e-01, 1.56313219e-01,\n",
       "         8.42030088e-02, 4.53585866e-02, 2.44338226e-02],\n",
       "        [1.00000000e+00, 1.19598919e+00, 1.43039014e+00, 1.71073114e+00,\n",
       "         2.04601595e+00, 2.44701296e+00, 2.92660105e+00],\n",
       "        [1.00000000e+00, 5.21323328e-01, 2.71778012e-01, 1.41684218e-01,\n",
       "         7.38632880e-02, 3.85066551e-02, 2.00744176e-02],\n",
       "        [1.00000000e+00, 1.03370850e+00, 1.06855327e+00, 1.10457260e+00,\n",
       "         1.14180608e+00, 1.18029465e+00, 1.22008062e+00],\n",
       "        [1.00000000e+00, 6.05624320e-01, 3.66780817e-01, 2.22131383e-01,\n",
       "         1.34528168e-01, 8.14735303e-02, 4.93423514e-02],\n",
       "        [1.00000000e+00, 7.94153164e-01, 6.30679248e-01, 5.00855921e-01,\n",
       "         3.97756314e-01, 3.15879436e-01, 2.50856653e-01],\n",
       "        [1.00000000e+00, 2.86693215e-01, 8.21929997e-02, 2.35641754e-02,\n",
       "         6.75568920e-03, 1.93681026e-03, 5.55270360e-04],\n",
       "        [1.00000000e+00, 3.15849386e-02, 9.97608343e-04, 3.15093982e-05,\n",
       "         9.95222407e-07, 3.14340386e-08, 9.92842177e-10],\n",
       "        [1.00000000e+00, 6.83829328e-01, 4.67622549e-01, 3.19774013e-01,\n",
       "         2.18670849e-01, 1.49533539e-01, 1.02255420e-01],\n",
       "        [1.00000000e+00, 2.65338670e-01, 7.04046095e-02, 1.86810654e-02,\n",
       "         4.95680904e-03, 1.31523312e-03, 3.48982205e-04],\n",
       "        [1.00000000e+00, 5.45791154e-03, 2.97887984e-05, 1.62584627e-07,\n",
       "         8.87372511e-10, 4.84320067e-12, 2.64337609e-14],\n",
       "        [1.00000000e+00, 1.69055692e-01, 2.85798272e-02, 4.83158247e-03,\n",
       "         8.16806521e-04, 1.38085792e-04, 2.33441892e-05],\n",
       "        [1.00000000e+00, 2.32251615e-01, 5.39408127e-02, 1.25278409e-02,\n",
       "         2.90961127e-03, 6.75761918e-04, 1.56946797e-04],\n",
       "        [1.00000000e+00, 6.49959996e-01, 4.22447996e-01, 2.74574298e-01,\n",
       "         1.78462310e-01, 1.15993362e-01, 7.53910451e-02],\n",
       "        [1.00000000e+00, 8.51046910e-01, 7.24280843e-01, 6.16396973e-01,\n",
       "         5.24582740e-01, 4.46444520e-01, 3.79945229e-01],\n",
       "        [1.00000000e+00, 2.43720134e-01, 5.93995036e-02, 1.44768550e-02,\n",
       "         3.52830103e-03, 8.59918000e-04, 2.09579330e-04],\n",
       "        [1.00000000e+00, 6.86092122e-01, 4.70722400e-01, 3.22958931e-01,\n",
       "         2.21579578e-01, 1.52024003e-01, 1.04302471e-01],\n",
       "        [1.00000000e+00, 8.79130291e-01, 7.72870069e-01, 6.79453489e-01,\n",
       "         5.97328144e-01, 5.25129265e-01, 4.61657043e-01]]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_size = 20 # 训练集大小\n",
    "test_size = 12 # 测试集大小\n",
    "train_X = np.random.uniform(low=0, high=1.2, size=train_size) # 训练集特征,low表示下限,high表示上限,size表示样本大小,特征值在0到1.2之间,样本大小为20\n",
    "\n",
    "test_X = np.random.uniform(low=0.1, high=1.3, size=test_size) # 测试集特征\n",
    "\n",
    "train_y = np.sin(train_X * 2 * np.pi) + np.random.normal(0, 0.2, train_size) # 训练集目标变量\n",
    "test_y = np.sin(test_X * 2 * np.pi) + np.random.normal(0, 0.2, test_size) # 测试集目标变量\n",
    "\n",
    "poly = PolynomialFeatures(6) # 次数为6\n",
    "\n",
    "train_poly_X = poly.fit_transform(train_X.reshape(train_size,1)) #作用是将训练集特征转换为多项式特征,添加一项偏置项1，后续分别为每一项的n次幂，次数逐渐从1增加到6\n",
    "\n",
    "train_X,train_X.reshape(train_size,1),train_poly_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2418793620049859\n",
      "0.15230920433842318\n"
     ]
    }
   ],
   "source": [
    "test_poly_X = poly.fit_transform(test_X.reshape(test_size,1))\n",
    "\n",
    "model = Ridge(alpha=1) # 正则化参数为1\n",
    "model.fit(train_poly_X, train_y)\n",
    "train_pred_y = model.predict(train_poly_X)\n",
    "test_pred_y = model.predict(test_poly_X)\n",
    "\n",
    "print(mean_squared_error(train_y, train_pred_y)) # 训练集均方误差\n",
    "print(mean_squared_error(test_y, test_pred_y)) # 测试集均方误差\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import Lasso #Lasso正则（L1正则）,额外演示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6025488373277657\n",
      "0.4611229363114366\n"
     ]
    }
   ],
   "source": [
    "model = Lasso(alpha=1)\n",
    "model.fit(train_poly_X, train_y)\n",
    "train_pred_y = model.predict(train_poly_X)\n",
    "test_pred_y = model.predict(test_poly_X)\n",
    "\n",
    "print(mean_squared_error(train_y, train_pred_y)) # 训练集均方误差\n",
    "print(mean_squared_error(test_y, test_pred_y)) # 测试集均方误差\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
